package com.wyzz.hr.bo.impl;

import java.util.Collection;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOutmatchModelBO;
import com.wyzz.hr.dao.IOutmatchModelDAO;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.OutmatchModel;

public class OutmatchModelBOImpl implements IOutmatchModelBO {

	public OutmatchModelBOImpl() {
	}

	public IOutmatchModelDAO getDao() {
		return dao;
	}

	public void setDao(IOutmatchModelDAO dao) {
		this.dao = dao;
	}

	public OutmatchModel loadObject(String id) {
		return loadObject(id, null);
	}

	public OutmatchModel loadObject(String id, String fetch[]) {
		return (OutmatchModel) dao
				.loadObject(com.wyzz.hr.pojo.OutmatchModel.class, id, fetch,
						new boolean[0]);
	}

	public OutmatchModel loadDefaultObject(Iodef iodef) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(OutmatchModel.class);
		detachedCriteria.add(Restrictions.eq(OutmatchModel.PROP_OMM_IO, iodef));
		detachedCriteria.add(Restrictions.eq(OutmatchModel.PROP_OMM_DEFAULT,
				Integer.valueOf(1)));
		return (OutmatchModel) dao.findByCriteria(detachedCriteria, 1, 1)
				.get(0);
	}

	public OutmatchModel getObject(Iodef iodef, String ommId) {
		OutmatchModel result = loadObject(ommId);
		if (result != null
				&& result.getOmmIo().getIoId().compareTo(iodef.getIoId()) == 0)
			return result;
		else
			return null;
	}

	public boolean saveObject(OutmatchModel obj) {
		dao.saveObject(obj);
		return true;
	}

	public boolean updateObject(OutmatchModel obj) {
		dao.updateObject(obj);
		return true;
	}

	public boolean saveOrUpdateOutmatchModel(OutmatchModel omm, List omDelList) {
		dao.saveOrupdate(omm);
		if (omDelList != null && omDelList.size() > 0)
			dao.deleteCollection(omDelList);
		if (omm.getOmList() != null && omm.getOmList().size() > 0)
			dao.saveOrupdate(omm.getOmList());
		return true;
	}

	public boolean saveOrUpdate(Collection outmatchModels) {
		dao.saveOrupdate(outmatchModels);
		return true;
	}

	public boolean deleteObject(String id) {
		dao.deleteObject(com.wyzz.hr.pojo.OutmatchModel.class, id);
		return true;
	}

	public boolean deleteOutmatchModel(OutmatchModel outmatchModel) {
		dao.deleteCollection(outmatchModel.getOmList());
		dao.deleteObject(outmatchModel);
		return true;
	}

	public List getListByIodef(Iodef iodef) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchModel.class);
		detachedCriteria.add(Restrictions.eq(OutmatchModel.PROP_OMM_IO, iodef));
		detachedCriteria.addOrder(Order.asc(OutmatchModel.PROP_OMM_NAME));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public List getListByIodef(String ioName) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchModel.class);
		detachedCriteria
				.setFetchMode(OutmatchModel.PROP_OMM_IO, FetchMode.JOIN);
		detachedCriteria.createAlias(OutmatchModel.PROP_OMM_IO, "ommIo");
		detachedCriteria.add(Restrictions.eq("ommIo.ioName", ioName));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	private IOutmatchModelDAO dao;
}
